package com.itheima.prize.commons.db.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.prize.commons.db.entity.CardProductDto;
import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.entity.CardUserDto;
import com.itheima.prize.commons.db.service.CardUserService;
import com.itheima.prize.commons.db.mapper.CardUserMapper;
import com.itheima.prize.commons.db.service.GameLoadService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PasswordUtil;
import com.itheima.prize.commons.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
* @author shawn
* @description 针对表【card_user(会员信息表)】的数据库操作Service实现
* @createDate 2023-12-26 11:58:48
*/
@Service
public class CardUserServiceImpl extends ServiceImpl<CardUserMapper, CardUser>
    implements CardUserService{

    @Autowired
    private GameLoadService gameLoadService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 会员登录
     * @param request
     * @param account
     * @param password
     * @return
     */
    @Override
    public ApiResult login(HttpServletRequest request, String account, String password) {
        if(redisUtil.get(account)==null){
            redisUtil.set(account,0);
        }
        int count = (int)redisUtil.get(account);
        //密码错误5次，请5分钟后再登录
        if(count>4){
            redisUtil.expire(account,5*60);
        return new ApiResult(0 ,"密码错误5次，请5分钟后再登录",null);
    }
       // 调用mapper层查询会员信息
        CardUser cardUser = baseMapper.selectOne(new QueryWrapper<CardUser>()
                .eq("passwd",PasswordUtil.encodePassword(password))
                .eq("uname",account));
        //账户名或密码错误
        if(cardUser == null) {
            redisUtil.incr(account,1);
            return new ApiResult(0, "账户名或密码错误", null);
        }
        //登录成功
        cardUser.setPasswd(null);
        cardUser.setIdcard(null);
        //设置session
        HttpSession session = request.getSession();
        session.setAttribute("user",cardUser);
        //删除redis中存储的登录次数
        redisUtil.del(account);
        //返回会员信息
        return new ApiResult(1, "登录成功", cardUser);
    }

    /**
     * 会员退出
     * @param request
     * @return
     */
    @Override
    public ApiResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return new ApiResult(1,"退出成功",null);
    }

    /**
     * 获取会员信息
     * @param request
     */
    @Override
    public ApiResult<CardUserDto> getUserInfo(HttpServletRequest request) {
        //从session获取会员信息
        HttpSession session = request.getSession();
        CardUser user =(CardUser) session.getAttribute("user");
        Integer id = user.getId();
        CardUserDto cardUserDto = new CardUserDto();
        BeanUtils.copyProperties(user,cardUserDto);
        //设置游戏和商品数量
        cardUserDto.setGames(gameLoadService.getGamesNumByUserId(id));
        cardUserDto.setProducts(gameLoadService.getPrizesNumByUserId(id));
        //返回结果
        return new ApiResult<>(1,"成功",cardUserDto);
    }
}




